package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-07
 */
@Transactional
@Service
public class NursingPlanServiceImpl implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private static final String NURSING_PLAN_KEY = "nursing:plan";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //根据护理计划的id查询对应的护理项目列表
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */

    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        try {
            //保存护理计划  属性拷贝(属性名称和类型一致，就可以进行拷贝)
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);

            nursingPlan.setCreateTime(DateUtils.getNowDate());
            nursingPlanMapper.insertNursingPlan(nursingPlan);

            //保存护理计划与护理项目的中间表  批量
            int count = nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
            //清除缓存
            stringRedisTemplate.opsForHash().delete(NURSING_PLAN_KEY);
            return count == 0 ? 0 : 1;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            // 属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);

            //判断dto的list（护理计划与护理项目的关联关系）是否为空，不为空，删除之前的关联关系，再重新批量添加
            if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
                //删除之前的关联关系  根据护理计划ID删除
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
                //批量添加
                nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
            }

            //不管dto的list空不空，都要修改护理计划
            int i = nursingPlanMapper.updateNursingPlan(nursingPlan);
            //清除缓存
            stringRedisTemplate.opsForHash().delete(NURSING_PLAN_KEY);
            return i;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        int i = nursingPlanMapper.deleteNursingPlanByIds(ids);
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_PLAN_KEY);
        return i;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        int i = nursingPlanMapper.deleteNursingPlanById(id);
        //清除缓存
        stringRedisTemplate.opsForHash().delete(NURSING_PLAN_KEY);
        return i;
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> listAll() {
        //先查询redis缓存
        Map<Object, Object> nursingPlans = stringRedisTemplate.opsForHash().entries(NURSING_PLAN_KEY);
        //判断缓存是否为空
        if (!nursingPlans.isEmpty()) {
            //缓存命中
            List<NursingPlan> shopList = new ArrayList<>();
            nursingPlans.forEach((k, v) -> {
                //json字符串转为对象
                NursingPlan nursingPlan = JSON.parseObject(v.toString(), NursingPlan.class);
                shopList.add(nursingPlan);
            });
            return shopList;
        }
        //查询数据库
        List<NursingPlan> Plans = nursingPlanMapper.listAll();
        //处理数据
        LinkedHashMap<Object, Object> PlanMap = new LinkedHashMap<>();
        Plans.forEach(plan -> {
            //plan转为json字符串
            String json = JSONUtil.toJsonStr(plan);
            PlanMap.put(plan.getId().toString(), json);
        });
        //存入redis缓存, 过期时间设置为1天
        stringRedisTemplate.opsForHash().putAll(NURSING_PLAN_KEY, PlanMap);
        stringRedisTemplate.expire(NURSING_PLAN_KEY, 1, TimeUnit.DAYS);
        //返回
        return Plans;
    }
}
